
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>广告位置管理</title>
    #include("../../common/res.html")
</head> 
<body class="easyui-layout" data-options="fit:true" style="height:100%;">
    
    <div class="container" data-options="region:'center',border:false" style="height:100%;">
      <table  id="dg" style="width:100%;height:100%;"></table>
      <div id="tb" style="padding:0 30px;">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-redo" id="btn_enable">启用</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-undo" id="btn_disable">禁用</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" id="btn_add">添加</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" id="btn_edit">编辑</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" id="btn_remove">删除</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" id="btn_role_settings">分配角色</a>
      </div>
    </div>
    <div id="wu-dialog"></div>
    
    <script type="text/javascript">
        $(function(){
            $('#dg').datagrid({
                url:'#(ctx)/admin/user/getData',
                idField:'id',
                method:'get',
                toolbar:"#tb",
                fitColumns:false,
                rownumbers:true,
                singleSelect:true,
                pagination:true,
                striped:true,
                checkOnSelect:false,
                selectOnCheck:false,
                fit:true,
                pageSize:20,
                sortName:'id',
                columns:[[
                    {field:'ck',checkbox:true},
                    {field:'id',title:'id',width:100},
                    {field:'name',title:'用户名',width:100},
                    {field:'roleNames',title:'拥有角色',width:300},
                    {field:'des',title:'描述',width:100},
                    {field:'email',title:'邮箱',width:200},
                    {field:'phone',title:'电话号码',width:160},
                    {field:'status',title:'状态',width:80,formatter:function(val){if(val==1){return '启用';}else{return '禁用'}}},
                ]],
                onLoad:function(){
                    
                },
            }); 
            $("#btn_search").click(function(){
                $('#dg').datagrid('load',{
                    phone:$("#phone").val(),
                    id:$("#id").val()
                });
            });
            $("#btn_add").click(function(){
            	$('#wu-dialog').dialog({
					closed: false,
					modal:true,
                    title: "添加用户",
                    width: 800,    
                    height: 500,
                    iconCls:'icon-save',
                    href: '#(ctx)/admin/user/edit',  
					buttons: [{
						text: '确定',
						iconCls: 'icon-ok',
						handler: function(){
							$('#EditForm').submit();
						}
					}, {
						text: '取消',
						iconCls: 'icon-cancel',
						handler: function () {
							$('#wu-dialog').dialog('close');                    
						}
                    }],
                    onLoad:function(){
                        $('#EditForm').form({    
                            url:"#(ctx)/admin/user/saveUser",    
                            onSubmit: function(data){
                                
                            },    
                            success:function(data){    
                                data=JSON.parse(data);
                                _toast(data.data);
                                if(data.code==0){
                                	$('#dg').datagrid("reload");
                                }
                                $('#wu-dialog').dialog('close');
                            }    
                        });
                    }
				});
            });
            $("#btn_edit").click(function(){
            	 var row=$('#dg').datagrid("getSelected");
                 if(!row){
                     _toast("请选中一行");
                     return;
                 }
            	$('#wu-dialog').dialog({
					closed: false,
					modal:true,
                    title: "编辑用户",
                    width: 800,    
                    height: 500,
                    iconCls:'icon-save',
                    href: '#(ctx)/admin/user/edit',  
					buttons: [{
						text: '确定',
						iconCls: 'icon-ok',
						handler: function(){
							$('#EditForm').submit();
						}
					}, {
						text: '取消',
						iconCls: 'icon-cancel',
						handler: function () {
							$('#wu-dialog').dialog('close');                    
						}
                    }],
                    onLoad:function(){
                    	$('#EditForm').form('load',row);
                    	$("#pwd").textbox("clear");
                        $('#EditForm').form({    
                            url:"#(ctx)/admin/user/saveUser",    
                            onSubmit: function(data){
                                
                            },    
                            success:function(data){    
                                data=JSON.parse(data);
                                _toast(data.data);
                                if(data.code==0){
                                	$('#dg').datagrid("reload");
                                }
                                $('#wu-dialog').dialog('close');
                            }    
                        });
                    }
				});
            });
           
            //分配角色
            $("#btn_role_settings").click(function(){
            	var rows=$('#dg').datagrid("getChecked");
                var ids=[];
                for(var i in rows){
                    ids.push(rows[i].id);
                }
                if(rows.length==0){
                    $.messager.show({
                        title:'消息提示',
                        msg:"请选中一行",
                    });
                    return;
                }
                if(rows.length>1){
                    $.messager.show({
                        title:'消息提示',
                        msg:"最多只能选中一行",
                    });
                    return;
                }
                layer.open({
					title:'给用户【'+rows[0].name+'】分配角色',
				    type: 2,
				    area: ['380px', '530px'],
				    fix: false, //不固定
				    maxmin: true,
				    content: '#(ctx)/admin/user/userRoleSetting?uid='+ids[0]
				});
            })
            $("#btn_enable").click(function(){
            	setVisible(1);
            })
            $("#btn_disable").click(function(){
            	setVisible(0);
            })
            $("#btn_remove").click(function(){
            	var rows=$('#dg').datagrid("getChecked");
                var ids=[];
                for(var i in rows){
                    ids.push(rows[i].id);
                }
                if(rows.length==0){
                    _toast("请勾选一行");
                    return;
                }
                $.messager.confirm('确认对话框', '是否删除勾选项？', function(r){
                    if (r){
                      $.post("#(ctx)/admin/user/del",{ids:ids.join(',')},function(data){
                    	  _toast(data.msg);
                          if(data.code==0){
                        	  reloadGrid();
                          }
                      },'json');
                    }
                });

            });
        });
        function reloadGrid(){
        	$('#dg').datagrid("reload");
        }
        function setVisible(visible){
			var rows=$('#dg').datagrid("getChecked");
            var ids=[];
            for(var i in rows){
                ids.push(rows[i].id);
            }
            if(rows.length==0){
                _toast("至少选中一行");
                return;
            }
            var idstr=ids.join(",");
            var data={ids:idstr,visible:visible}
            $.post("#(ctx)/admin/user/setVisible",data,function(data){
            	_toast(data.data);
            	if(data.code==0){
            		reloadGrid();
            	}
            },'json');
        }
    </script>
</body> 
</html>